/**************************************************
 * Project Name         [JavaEE_XML]
 * File Name            [JDOMAPI.java]
 * Creation Date        [08-21-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 **************************************************/
package pkg_04_jdom;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.DOMBuilder;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.xml.sax.SAXException;

/**
 * JDOM API
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class JDOMAPI {

	/**
	 * 创建XML文档
	 * 
	 * @throws IOException
	 * @throws JDOMException
	 */
	public static void createXMLDocument() throws IOException, JDOMException {
		// 创建Root Element
		Element rootElement = new Element("Students");
		Element studentElement = new Element("Student");
		Element nameElement = new Element("Name");
		Element ageElement = new Element("Age");
		Element genderElement = new Element("Gender");
		Element birthdayElement = new Element("Birthday");

		Attribute attr = new Attribute("student_id", "00004");
		studentElement.setAttribute(attr);
		nameElement.setText("Sean");
		ageElement.setText("20");
		genderElement.setText("男");
		birthdayElement.setText("1984-03-24");

		studentElement.addContent(nameElement);
		studentElement.addContent(ageElement);
		studentElement.addContent(genderElement);
		studentElement.addContent(birthdayElement);

		rootElement.addContent(studentElement);

		Document document = new Document(rootElement);

		XMLOutputter out = new XMLOutputter();
		Format format = Format.getPrettyFormat();
		format.setEncoding("UTF-8");
		format.setIndent("    ");// 设置缩进
		out.setFormat(format);
		out.output(document, new FileOutputStream(new File(
				"src/xml/Student_JDOM.xml")));

	}

	/**
	 * 使用DOM解析
	 * 
	 * @throws IOException
	 * @throws JDOMException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	public static void parseXMLDocumentByDOM() throws IOException,
			JDOMException, ParserConfigurationException, SAXException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		org.w3c.dom.Document w3cDocument = builder.parse(new File(
				"src/xml/Student_JDOM.xml"));

		DOMBuilder jdomBuilder = new DOMBuilder();
		Document jdomDocument = jdomBuilder.build(w3cDocument);

		Element rootElement = jdomDocument.getRootElement();
		System.out.println("Root element = " + rootElement.getName());

		treeWalk(rootElement);
	}

	/**
	 * 使用SAX解析
	 * 
	 * @throws IOException
	 * @throws JDOMException
	 */
	public static void parseXMLDocumentBySAX() throws JDOMException,
			IOException {
		SAXBuilder saxBuilder = new SAXBuilder();
		Document document = saxBuilder.build(new File(
				"src/xml/Student_JDOM.xml"));

		Element rootElement = document.getRootElement();
		System.out.println("Root element = " + rootElement.getName());

		treeWalk(rootElement);
	}

	/**
	 * 递归打印Element
	 * 
	 * @param element
	 */
	private static void treeWalk(Element element) {
		List<Element> childrenElements = element.getChildren();
		for (Element ce : childrenElements) {
			List<Element> tempElements = ce.getChildren();
			if (tempElements != null && tempElements.size() > 0) {
				printElementAndAttribute(ce, true);
				treeWalk(ce);
			} else {
				printElementAndAttribute(ce, false);
			}
		}

	}

	private static void printElementAndAttribute(Element ce, boolean hasChild) {
		if (hasChild) {
			System.out.print(ce.getName());
		} else {
			System.out.print(ce.getName() + " = " + ce.getValue());
		}
		List<Attribute> attrs = ce.getAttributes();
		for (int i = 0; attrs != null && i < attrs.size(); i++) {
			System.out.print(" Attribute[" + attrs.get(i).getName() + " = "
					+ attrs.get(i).getValue() + "]");
		}
		System.out.println();
	}

	public static void main(String[] args) {
		try {
			// createXMLDocument();

			parseXMLDocumentByDOM();
			System.out.println();
			parseXMLDocumentBySAX();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
